package com.fangshicai.controller;

import com.fangshicai.bean.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

/**
 * @ClassName IndexController
 * @Description TODO
 * @Author fangshicai
 * @Date 2021/7/16 8:45
 */
@Controller
public class IndexController {

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @GetMapping({"/login","/"})
    public String loginPage(){


        return "login";
    }

    //重定向防止表单重复提交
    @PostMapping("/login")
    public String main(User user, HttpSession session, Model model){


        System.out.println(user);
        if(StringUtils.hasLength(user.getUserName())&&"123456".equals(user.getPassWord())){

            session.setAttribute("loginUser",user);
            return "redirect:main.html";
        }
        else {
            model.addAttribute("msg","账号密码错误");
            return "login";
        }


    }
    @GetMapping("/main.html")
    public String mainPage(HttpSession session,Model model){
//        User loginUser = (User) session.getAttribute("loginUser");
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        String s = ops.get("/main.html");
        model.addAttribute("countMain",s);

        System.out.println(s);
//        if(loginUser!=null){
            return "main";

//        }else {
//            model.addAttribute("msg","请先登陆");
//            return "/login";
//        }
    }
    @GetMapping("/myIndex")
    public String gomainPage(){

            return "myindex";

    }
    @RequestMapping("/my11dynamic_table")
    public String gomydynamic_tablePage(){

            return "mydynamic_table";

    }




}
